Page({
  data: {
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  globalData: {
    userInfo: {}
  },
  onLoad: function (options) {

  this.setData({
    page: options.page
  })
    console.log("获取信息开始")
  },
  toindex:function(){
    wx.reLaunch({
      url: '/pages/class/classes'
    })
  },
  // 获取设置信息
  getset: function () {

    var user = { info: {} }
    // 获取用户信息
    wx.getSetting({
      success: function (res) {
        console.log("获取设置信息成功")
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              console.log("获取用户信息成功")
              // var that = getApp();
              console.log(res.userInfo)
              user.info.userinfo = res.userInfo
              // 登录
              wx.login({
                success: res => {
                  console.log(res)
                  // var that = getApp();
                  // that.code = res.code
                  user.code = res.code
                  var app = getApp()
                  var testurl = app.testUrl
                  wx.request({
                    url: testurl + '/user/getcode.do?code=',
                    data: {
                      code: res.code
                    },
                    header: {
                      'content-type': 'application/json' // 默认值
                    },
                    success: function (res) {
                      var app = getApp()
                      console.log(res)
                      user.info.openid = res.data.openid
                      user.info.userinfo.area = app.getc.getChina(user.info.userinfo.province) + " " + app.getc.getChina(user.info.userinfo.city),
                        console.log(user)
                      wx.setStorage({
                        key: "user",
                        data: user.info
                      })

                      // 设置用户信息
                      app.globalData.userInfo = user.info.userinfo
                      app.globalData.openid = user.info.openid
                      wx.request({
                        url: testurl + '/user/add.do',
                        method: "POST",
                        data: {
                          wxOpenid: user.info.openid,
                          wxUsername: user.info.userinfo.nickName,
                          userImg: user.info.userinfo.avatarUrl,
                          area: user.info.userinfo.area,
                          username: user.info.userinfo.nickName
                        },
                        header: {
                          'content-type': 'application/json' // 默认值
                        },
                        success: function (res) {
                          console.log(res)
                          var app = getApp()
                          console.log(app)
                        }
                      })

                    }
                  })
                  // 发送 res.code 到后台换取 openId, sessionKey, unionId
                },
                fail: function (res) {
                  console.log(res + "失败")
                }
              })


            },
            fail: function (e) {
              console.log(e + "获取用户信息失败")
              wx.navigateTo({
                url: '/pages/msg/msg_fail'
              })
            }
          })
        } else {
          console.log('没有获取授权')
          wx
        }
      },
      fail: function () {
        console.log("获取设置信息失败")
      }
    })
  },
  getinfo:function(e){
    console.log(e)
    var page=this.data.page
    // this.getset();
    wx.reLaunch({
      url: '/pages/class/classes'
    })
    // if(page==2){
    //   console.log(2222)
    //   wx.reLaunch({
    //     url: '/pages/dongtai/dongtai'
    //   })
    // }else if(page==3){
    //   wx.reLaunch({
    //     url: '/pages/user/user'
    //   })
    // }else{
    //   wx.reLaunch({
    //     url: '/pages/class/classes'       
    //   })
    // } 
  }
})


